#!/bin/ksh
#(C) COPYRIGHT International Business Machines Corp. 1991, 1992, 1993, 1994.
# All Rights Reserved.  Licensed Materials - Property of IBM
############################################################################
#
# Note:  The code for progress meter reads the size for the file from
#        the disk directly using 'dosdir -l' command.  This command is
#        used for files that have not been stored using 'doswrite -a'.
#        The files that are stored in the dos format have their name and
#        file size stored in $filename.  Any changes to the size of dos
#        formatted files will require a programmer to change the size of
#        these files in this script.
#
############################################################################

# Dec 05 94 SLL - Moved help file to disk 4
# Aug 01 94 SLL - Made samples.doc a dos file
# Jul 14 94 SLL - Removed cfg.ico
# Mar 01 94 SLL - Moved Samples to their own diskette
# Dec 08 93 SLL - Updated for V1R3.0.0 changes
# Oct 22 93 Sam - no longer compress samples.doc
# Oct 01 93 Sam - placed escape sequence before each dollar sign for AIX 3.2.4



#       The following lines check to see if the user has 10 meg of disk
#       space available on their hard disk.  A directory for installing
#       software is also created if one has not been created by the user.
#************************************************************************

while true
do
        clear
        done=false
        currentpath=`pwd`
        showpath=`echo $currentpath | sed 's/$/\/cfg130/'`
        echo 'Press ENTER to accept the default directory path'
        echo '['$showpath']'
        echo 'for the Configuration Program or type the desired path.'
        echo ': \c'; read installpath

        if [ -z "$installpath" ]; then
                installpath=$showpath
        fi

        if echo "$installpath" | grep '/' > /dev/null; then
                truncpath=`echo $installpath | sed 's/\/[A-Za-z0-9\.\_]*$//g'`
                firstchar=`echo $truncpath | cut -c1`
                if [ "$firstchar" = $ ]; then
                        truncpath=`eval echo \$truncpath`
                        installpath=`eval echo \$installpath`
                fi
        else
                truncpath=$currentpath
                installpath=`echo $currentpath'/'$installpath`
        fi

        index=1
                for argu in `df -I $truncpath | sed '1 d'`; do
                        if [ "$index" -eq 4 ]; then
                                value=$argu
                        fi
                        index=`expr $index + 1`
                done

                # if 10 Meg space available
                if [ "$value" -gt 10000 ]; then
                   if [ ! -d "$installpath" ]; then
                      mkdir $installpath
                      done=true; cd $installpath
                   else
                      cd $installpath

                      #checking to see if the older version of
                      if [ -z "$(ls -l | grep cfg.app)" ]; then
                         echo $installpath
                         done=true
                      else
                         echo
                         echo 'Previous version of MPNP Configuration \c'
                         echo 'program found in path'
                         echo '['$installpath']'
                         echo 'Do you wish to overwrite the old version. \c'
                         echo 'Press (y or n) and then press ENTER \c'
                         read command
                         if [ "$command" = y ]; then
                            done=true
                         else
                            cd ..
                         fi
                      fi
                  fi
                else
                        echo
                        echo 'Error: Not enough space in ['$installpath'] \c'
                        echo 'file system.'
                        echo '       Please install Configuration Program in \c'
                        echo 'another directory. \007'
                        exit 1
                fi

                if [ \( -f /usr/bin/dosread  -a  -f /usr/bin/doswrite \)  -a \( -f /usr/bin/dosdir  -a  -f /usr/bin/csh \) ]
                then
                        if test "$done" = true; then
                                break 3
                        fi
                else
                        if [ \( -f /bin/dosread  -a  -f /bin/doswrite \) -a \( -f /bin/dosdir  -a  -f /bin/csh \) ]
                        then
                                if test "$done" = true; then
                                        break 4
                                fi
                        fi
                        echo
                        echo 'Error: Could not find the AIX commands, \c'
                        echo 'dosread, doswrite, dosdir or csh'
                        echo 'in the /bin or /usr/bin directories. These \c'
                        echo 'four commands must be installed'
                        echo 'to run the MPNP Configuration Program on AIX \c'
                        echo 'systems. \007'
                        exit 1
                fi
done

#       Disk copy header
##################################################################################
header () {
echo '    Filename                                 % Disk copied'
echo
}

#       Shows the file name.
################################################################################
showfilename () {
name=`echo $filename | cut -f1 -d:`
if [ -f "$name" ]; then
        chmod +w "$name"
        rm "$name"
fi

echo '   ' $name '\c'
}

#      Check to see if the file has started to be copied to harddisk.
##############################################################################
fileexist() {
while [ ! -f "$fname" ]; do
        sleep 1
done
}

#       Shows the progress meter while the file is being copied.
###############################################################################
showcopystatus() {
fname=`echo $filename | cut -f1 -d:`
filesize=`echo $filename | cut -f2 -d:`
tenpercent=`expr $filesize \/ 10`
copied=$tenpercent
echo '* \c'; fileexist

while [ "$copied" -le "$filesize" ]; do
        currfilesize=`ls -l $fname | cut -c31-41`
        if [ "$currfilesize" -ge "$copied" ]; then
                echo '* \c'
                copied=`expr $copied + $tenpercent`
        fi
done
}

#       Shows the percentage of disk copied.
#############################################################################
showfilestatus () {
fname=`echo $filename | cut -f1 -d:`
length=`echo $fname | wc -c`
length=`expr $length - 1`
length=`expr 15 - $length`
index=1
        while [ "$index" -ne "$length" ]; do
                echo ' \c'
                index=`expr $index + 1`
        done
        showcopystatus
        echo '       ' $percentage
wait
}

#       Gets the file size from the disk
###########################################################################
getfilesize () {
uppcasefilename=`echo $filename | tr '[a-z]' '[A-Z]'`
uppcasefilename=`echo $uppcasefilename '\040'`
size=`dosdir -l | grep "$uppcasefilename" | cut -c13-22`
filename=`echo $outfile:$size`
}

#       Appends .Z to file name and uncompresses the file.
##########################################################################
restorefile () {
# stripping off file size from file name
#newname=`echo $filename | sed 's/\: [0-9]*$//'`
# appending .Z to the file name
cfile=`echo $outfile | sed 's/$/.Z/'`
# changing the actual file name to filename.Z
mv "$outfile" "$cfile"
uncompress "$cfile"
}

#       The following lines prompt user for inserting the disk, and copy
#       the program files from disk to hard disk.
###########################################################################

clear
dosdir | grep INSTALL.AIX > /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 1.  Press the ENTER key when ready... \007'
 read junk
 dosdir | grep INSTALL.AIX > /dev/null
done
sample=false
echo 'Copy configuration samples from diskette. \c'
echo 'Press (y or n) and then press ENTER \c'
read ans
if [ "$ans" = y ]; then
   sample=true
fi
echo 'Copying files from Diskette 1 ...'
echo;  header

filename=cfg.ai$; outfile=cfg.aix; getfilesize; percentage='17%'
showfilename; dosread CFG.AI$ cfg.aix &
showfilestatus; restorefile
filename=XCFG; outfile=cfg; getfilesize; percentage='33%'
showfilename; dosread XCFG cfg &
showfilestatus
filename=AIX.INI; outfile=cfg.ini; getfilesize; percentage='50%'
showfilename; dosread AIX.INI cfg.ini &
showfilestatus
filename=cfg.ms$; outfile=cfg.msg; getfilesize; percentage='67%'
showfilename; dosread CFG.MS$ cfg.msg &
showfilestatus; restorefile
filename=cfg.lg$; outfile=cfg.lgo; getfilesize; percentage='83%'
showfilename; dosread CFG.LG$ cfg.lgo &
showfilestatus; restorefile
filename=README:60739; percentage='100%'
showfilename; dosread -a README README &
showfilestatus
echo; echo 'Done reading files from Diskette 1.'

echo; echo 'Please insert Diskette 2.  Press the ENTER key when ready... \007'
read junk

dosdir | grep 'CFG.1\$'> /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 2.  Press the ENTER key when ready...\007'; read junk
 dosdir | grep 'CFG.1\$'> /dev/null
done

echo 'Copying files from Diskette 2 ...'
echo; header
filename=cfg.1$; outfile=cfg.1; getfilesize; percentage='100%'
showfilename; dosread CFG.1$ cfg.1 &
showfilestatus
echo; echo 'Done reading files from Diskette 2.'
echo; echo 'Please insert Diskette 3.  Press the ENTER key when ready...\007'; read junk

dosdir | grep 'CFG.2\$'> /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 3.  Press the ENTER key when ready... \007'; read junk
 dosdir | grep 'CFG.2\$'> /dev/null
done

echo 'Copying files from Diskette 3 ...'
echo; header

   filename=cfg.2$; outfile=cfg.2; getfilesize; percentage='100%'
   showfilename; dosread CFG.2$ cfg.2 &
   showfilestatus; cat cfg.1 cfg.2 > cfg.app
   rm cfg.1 cfg.2; outfile=cfg.app; restorefile

echo; echo 'Done reading files from Diskette 3.\007'

echo; echo 'Please insert Diskette 4.  Press the ENTER key when ready...\007'; read junk

dosdir | grep 'CFG.HL\$' > /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 4.  Press the ENTER key when ready... \007'; read junk
 dosdir | grep 'CFG.HL\$' > /dev/null
done

echo 'Copying files from Diskette 4 ...'
echo; header

# copy the samples database and doc file if the user wants them
if [ "$sample" = true ]; then
   filename=cfg.hl$; outfile=cfg.hlp; getfilesize; percentage='73%'
   showfilename; dosread CFG.HL$ cfg.hlp &
   showfilestatus; restorefile

   filename=samples.cd$; outfile=samples.cdb; getfilesize; percentage='99%'
   showfilename; dosread samples.cd$ samples.cdb &
   showfilestatus; restorefile

   filename=samples.doc:3801; percentage='100%'
   showfilename; dosread -a samples.doc samples.doc &
   showfilestatus

else
   filename=cfg.hl$; outfile=cfg.hlp; getfilesize; percentage='100%'
   showfilename; dosread CFG.HL$ cfg.hlp &
   showfilestatus; restorefile
fi

echo; echo 'Done reading files from Diskette 4.\007'

chmod ugo+x cfg.aix
chmod ugo+x cfg

cd $currentpath
chmod u+w install.aix
rm install.aix
cd $installpath

echo; echo 'Installation of version 1 Release 3 PTF NP00688 of the'
echo 'MPNP Configuration Program is complete.\007'

